<?php

namespace Biz\model\brandActivity;

use Biz\model\NewProduct\NewProductWikiModel;
use Doraemon\pockets\datebase\BaseDbModel;

/**
 * 品牌活动wiki-basic表
 * Class BrandActivityBasic
 * @package Biz\model\brandActivity
 */
class BrandActivityBasic extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_brand_activity';

    const _DATABASE_ = 'brand_activity';

    const _TABLE_NAME = 'brand_activity_basic';

    const ACTIVITY_TYPE_BLIND_BOX = 1;//活动类型，超级盲盒
    const ACTIVITY_TYPE_NEW_PRODUCT = 2;//活动类型，新品速递
    const ACTIVITY_TYPE_LIGHT_TEST = 3;//活动类型，好物轻测

    const ACTIVITY_TYPE_WIKI_RANKING = 99;//虚拟类型，榜单（品牌活动库，不存数据）

    const ACTIVITY_STATE_NORMAL = 1;//正常状态
    const ACTIVITY_STATE_DEL = 9;//删除状态

    const ACTIVITY_NAME = [
        self::ACTIVITY_TYPE_BLIND_BOX => '超级盲盒',
        self::ACTIVITY_TYPE_NEW_PRODUCT => '新品速递',
        self::ACTIVITY_TYPE_LIGHT_TEST => '好物轻测',
    ];

    //品牌活动关注线提示信息
    const ACTIVITY_FEED_MAP = [
        self::ACTIVITY_TYPE_BLIND_BOX => '中奖啦',
        self::ACTIVITY_TYPE_NEW_PRODUCT => '参与了新品速递中奖啦',
        self::ACTIVITY_TYPE_LIGHT_TEST => '成为了好物评测官',
    ];

    const ACTIVITY_MAP = [
        self::ACTIVITY_TYPE_BLIND_BOX,
        self::ACTIVITY_TYPE_NEW_PRODUCT,
        self::ACTIVITY_TYPE_LIGHT_TEST
    ];

    public static function getInfoById($id)
    {
        $info = self::getByWhere(['id' => $id]);

        return $info;
    }

    public static function getByWhere($where, $select = '*')
    {
        return self::get($select, $where);
    }

    /**
     * 根据basicId查询正在进行中的活动
     * @param $basicIds
     * @return array|bool
     */
    public static function getActivityByBasicIds($basicIds)
    {
        $now = time();
        $where = [
            'basic_id' => $basicIds,
            'start_time[<=]' => $now,
            'end_time[>=]' => $now,
            'activity_state' => self::ACTIVITY_STATE_NORMAL,
        ];

        return self::selectByWhere($where, '*');
    }

    public static function getLimitActivityByBasicIds($basicIds)
    {
        $now = time();
        $where = [
            'basic_id' => $basicIds,
            'start_time[<=]' => $now,
            'activity_state' => self::ACTIVITY_STATE_NORMAL,
        ];

        return self::selectByWhere($where, '*');
    }


    public static function selectByWhere($where, $select = '*')
    {
        return self::select($select, $where);
    }

    public static function getActivityByBrandIds($brandIds)
    {
        $now = time();
        $where = [
            'brand_id' => $brandIds,
            'start_time[<=]' => $now,
            'end_time[>=]' => $now,
            'activity_state' => self::ACTIVITY_STATE_NORMAL,
            'ORDER' => ['end_time' => 'DESC'],
        ];

        return self::selectByWhere($where, '*');
    }

    public static function getActivityByActivityIds($activityIds)
    {
        $now = time();
        $where = [
            'activity_id' => $activityIds,
            'start_time[<=]' => $now,
            'end_time[>=]' => $now,
            'activity_state' => self::ACTIVITY_STATE_NORMAL,
            'ORDER' => ['end_time' => 'DESC']
        ];
        return self::selectByWhere($where, '*');
    }

    public static function getActivityByIds($ids)
    {
        $where = [
            'id' => $ids,
            'activity_state' => self::ACTIVITY_STATE_NORMAL,
        ];
        return self::selectByWhere($where, '*');
    }

    /**
     * 获取所有正在进行中的活动 basic_id
     * @return array|bool
     */
    public static function getALLBasicIds()
    {
        $now = time();
        $where = [
            'start_time[<=]' => $now,
            'end_time[>=]' => $now,
            'activity_state' => self::ACTIVITY_STATE_NORMAL,
        ];
        $basicIds = self::selectByWhere($where, 'basic_id');

        return !empty($basicIds) ? array_unique($basicIds) : [];
    }

    /**
     * 更新品牌活动WIKI basic 信息
     * @param $activityId
     * @param $activityType
     * @param $brandId
     * @param $basicId
     * @param $startTime
     * @param $endTime
     * @param int $activity_state
     * @return bool|int|mixed|string
     */
    public static function updateActivityBasic($activityId, $activityType, $brandId, $basicId, $startTime, $endTime)
    {
        $data = [
            'activity_id' => $activityId,
            'activity_type' => $activityType,
            'brand_id' => $brandId,
            'basic_id' => $basicId,
            'start_time' => $startTime,
            'end_time' => $endTime,
            'activity_state' => self::ACTIVITY_STATE_NORMAL,
        ];
        $info = self::getByWhere([
            'activity_type' => $activityType,
            'activity_id' => $activityId,
            'basic_id' => $basicId,
        ]);
        if (!empty($info)) {
            $result = self::updateData($data, ['id' => $info['id']]);
        } else {
            $result = self::insertData($data);
        }

        return $result;
    }

    public static function updateData($data, $where)
    {
        if (!isset($data['edittime'])) {
            $data['edittime'] = date('Y-m-d H:i:s');
        }
        $ret = self::update($data, $where);
        if ($ret->errorCode() !== '00000') {
            return false;
        }

        return true;
    }

    public static function insertData($data)
    {
        if (!isset($data['addtime'])) {
            $data['addtime'] = date('Y-m-d H:i:s');
        }
        if (!isset($data['edittime'])) {
            $data['edittime'] = date('Y-m-d H:i:s');
        }
        $ret = self::insert($data);
        if ($ret->errorCode() !== '00000') {
            return false;
        }

        return self::id();
    }

    /**
     * 更新品牌活动WIKI basic 活动状态（取消，或则下线活动，更新状态为删除）
     * @param $activityId
     * @param $activityType
     * @param $basicId
     * @param $activityState
     * @return bool
     */
    public static function updateActivityState($activityId, $activityType, $basicId, $activityState)
    {
        $result = true;
        $info = self::getByWhere([
            'activity_type' => $activityType,
            'activity_id' => $activityId,
            'basic_id' => $basicId,
        ]);
        if (!empty($info)) {
            $result = self::updateData(['activity_state' => $activityState], ['id' => $info['id']]);
        }

        return $result;
    }

    public static function selectCountWhere($where)
    {
        return self::count('id', $where);
    }

    /**
     * 新品速递保存
     * @param $data
     * @return bool
     */
    public static function saveNewProActivity($data)
    {
        $where = ['activity_type' => $data['activity_type'], 'activity_id' => $data['activity_id']];
        $id = self::getByWhere($where, 'id');
        $up_data['start_time'] = strtotime($data['start_time']);
        $up_data['end_time'] = strtotime($data['end_time']);
        $up_data['activity_state'] = $data['activity_state'];
        $up_data['basic_id'] = $data['basic_id'];
        if ($id) {
            self::update($up_data, $where);
        } elseif ((int)$data['activity_state'] === self::ACTIVITY_STATE_NORMAL) {
            $up_data['activity_type'] = $data['activity_type'];
            $up_data['activity_id'] = $data['activity_id'];
            self::insert($up_data);
        }


        return true;
    }

    /**
     * 新品速递审核通过
     * @param $nid
     * @return bool|int|mixed|string
     */
    public static function auditNewProAct($nid)
    {
        $info = NewProductWikiModel::first(['id' => $nid]);
        if ($info) {
            return self::_insertNewProAct($info);
        }

        return true;
    }

    /**
     * 新品速递添加
     * @param $data
     * @return false|int|mixed|string
     */
    private static function _insertNewProAct($data)
    {
        $in_data['activity_id'] = $data['id'];
        $in_data['activity_type'] = self::ACTIVITY_TYPE_NEW_PRODUCT;
        $in_data['brand_id'] = $data['brand_id'];
        $in_data['basic_id'] = $data['basic_id'];
        $in_data['start_time'] = strtotime($data['start_time']);
        $in_data['end_time'] = strtotime($data['end_time']);
        $in_data['activity_state'] = self::ACTIVITY_STATE_NORMAL;

        return self::insertData($in_data);
    }

    /**
     * 获取sourceId
     * @param $actType
     * @param $activityId
     * @return string
     */
    public static function getSourceId($actType, $activityId): string
    {
        return $actType . '_' . $activityId;
    }


}